import { FormInstance } from 'antd';
import debounce from 'lodash/debounce';
import { makeAutoObservable, runInAction } from 'mobx';
import verificationCodeApi from '../../api/verificationCodeApi';
import { getDateText } from '../../utils/dateUtil';

export default class VerificationCode {

  loading: boolean;
  detailLoading: boolean;
  exporting: boolean;
  data: any[];
  record: any;
  detail: any;
  current: number;
  pageSize: number;
  total: number;
  conditions: object;

  constructor() {
    this.loading = false;
    this.detailLoading = false;
    this.exporting = false;
    this.data = [];
    this.record = null;
    this.detail = null;
    this.current = 0;
    this.pageSize = 10;
    this.total = 0;
    this.conditions = this.getDefaultConditions();
    makeAutoObservable(this);
  }

  async fetchData(mobile: string, usage: string, success: boolean, used: boolean, createdDate: any, pageNumber: number) {
    if (this.loading) {
      return;
    }

    const { startDate, endDate } = this.getDateRange(createdDate);
    runInAction(() => {
      this.loading = true;
    });
    const res: any = await verificationCodeApi.query(mobile, usage, success, used, startDate, endDate, pageNumber, this.pageSize);
    const list: any[] = res.data?.list || [];
    this.selectRecord(null);
    runInAction(() => {
      this.data = list;
      this.current = res.data?.current || 0;
      this.total = res.data?.total || 0;
      this.loading = false;
      this.conditions = {
        mobile,
        usage,
        success,
        used,
        createdDate,
        pageNumber
      };
    });
  }

  async fetchDetail() {
    if (this.detailLoading) {
      return;
    }

    runInAction(() => {
      this.detailLoading = true;
    });
    const res: any = await verificationCodeApi.find(this.record?.id);
    runInAction(() => {
      this.detail = res.data;
      this.detailLoading = false;
    });
  }

  async exportData(mobile: string, usage: string, success: boolean, used: boolean, createdDate: any) {
    if (this.exporting) {
      return;
    }

    const { startDate, endDate } = this.getDateRange(createdDate);
    runInAction(() => {
      this.exporting = true;
    });
    await verificationCodeApi.export(mobile, usage, success, used, startDate, endDate);
    runInAction(() => {
      this.exporting = false;
      this.conditions = {
        mobile,
        usage,
        success,
        used,
        createdDate,
        pageNumber: 0
      };
    });
  }

  selectRecord(record: any) {
    runInAction(() => {
      this.record = record
    });
  }

  setPageSize(pageSize: number) {
    runInAction(() => {
      this.pageSize = pageSize;
    });
  }

  getDateRange(date: any) {
    return {
      startDate: getDateText(date ? date[0] : null),
      endDate: getDateText(date ? date[1] : null)
    };
  }

  getDefaultConditions() {
    return {
      mobile: '',
      usage: '',
      success: '',
      used: '',
      createdDate: null,
      pageNumber: 0
    };
  }

  resetConditions(form: FormInstance<any>) {
    runInAction(() => {
      this.conditions = this.getDefaultConditions();
      debounce(() => form.resetFields(), 50)();
    });
  }

};
